﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wedo.Recognition.Domain.Models;
using Wedo.Recognition.Domain.ViewModels;
using Wedo.Utility.File;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Service;
using System.Web.Mvc;
using Wedo.Domain.Models;
using Wedo.Utility; 

namespace Wedo.Recognition.Service.BankImport
{

    public class HSBCBankImport : BaseBankImport
    {
        private string currency;


        public override IPBankData Parse(System.Data.DataRow row)
        {
            if (currency == null)
                currency = _Datatable.Rows[3][1].ToString();

            var model = base.Parse(row);
            if (model != null)
            {
                model.Currency = currency;
                model.ReceiveType = 1;
                model.ReceiveDate = GetReciveTime(row[3].ToString());
                string[] vals = null;
                if (currency == "CNY")
                {
                    vals = row[0].ToString()
                        .Split(new string[] { "\n" }, 3, StringSplitOptions.RemoveEmptyEntries);
                    if (vals != null || vals.Length > 2)
                    {
                        if (vals.Length == 3)
                        {
                            model.LCNumber = base.RemoveEmpty(vals[2]);
                        }
                    }
                }
                else
                {
                    vals = row[0].ToString()
                        .Split(new string[] { "\n" }, 4, StringSplitOptions.RemoveEmptyEntries);
                    if (vals != null || vals.Length > 2)
                    {
                        if (vals.Length > 3)
                            model.LCNumber =base.RemoveEmpty(vals[3]);
                    }
                }
                model.BankRemarkNum = vals[0].Replace("NONREF", "");
                model.Payer = vals[1].Replace("NONREF", "").Trim();

            }
            return model;
        }


        private DateTime? GetReciveTime(string time)
        {
            //"30/11/2012"
            string[] vals = time.Split('/');
            if (vals != null && vals.Length == 3)
            {
                string val = vals[2] + "-" + vals[1] + "-" + vals[0];
                DateTime t=DateTime.Now;
                if (DateTime.TryParse(val, out t)) {
                    return t;
                }
            }
            return null;
        }

        protected override string RemoveEmpty(string val)
        {
            return base.RemoveEmpty(val).Replace("NONREF","");
        }
    }
    public class SLBCBankImport : BaseBankImport
    {
        public override IPBankData Parse(System.Data.DataRow row)
        {
            if (row[9].ToString() == "Credit")
            {
                var model= base.Parse(row);
                if (model!=null)
                    model.ReceiveType = GetReceiveType(model.RecieTypeStr);
                return model;
            }
            else
                return null;
        }
 
    }

    public class PCBC_TelegraphicBankImport : BaseBankImport
    {

        public override IPBankData Parse(System.Data.DataRow row)
        {
            var model = base.Parse(row);
            if (model != null)
            {
                model.ReceiveType = 1;

                if (row[2] != null && row[2].ToString().IsNotNullOrWhiteSpace())
                {
                    string time = row[2].ToString().Insert(4, "-").Insert(7, "-");
                    model.ReceiveDate = time.TryParseTo<DateTime>();
                }
            }
            return model;
        }
    }

    public class PCBC_LetterOfCreditImport : BaseBankImport
    {
        private DateTime? ReceiveDate;

        public override IPBankData Parse(System.Data.DataRow row)
        {
            var model = base.Parse(row);
            if (model != null)
            {
                if (model.Currency.IsNullOrWhiteSpace())
                    return null;

                string strBankPaidIn = model.Currency.Substring(3);
                if (strBankPaidIn.IsNullOrWhiteSpace())
                    return null;
                var BankPaidIn = strBankPaidIn.TryParseTo<decimal>();
                if (!BankPaidIn.HasValue)
                    return null;

                model.BankPaidIn = BankPaidIn.Value;
                model.Currency = model.Currency.Substring(0, 3);
                if (ReceiveDate == null)
                {
                    string receiveTime = DataTable.Rows[0][0].ToString().Replace("进帐日期:", "");
                    ReceiveDate = receiveTime.TryParseTo<DateTime>();
                }
                model.ReceiveDate = ReceiveDate;
                model.ReceiveType =3;
                model.BankCode = GetBankCode(model.Currency);
            }
            return model;
        }

    }

    public class CommonBankImport : BaseBankImport
    {
        public override IPBankData Parse(System.Data.DataRow row)
        {
            var model = base.Parse(row);
            if (model != null)
                model.ReceiveType = GetReceiveType(model.RecieTypeStr);
            return model;
        }

    }

    public class BaseBankImport: ObjectFromExcelFileByIndex<IPBankData>
    {

        public List<Bank> Banks { get; set; }
        public List<SysParam> Params { get; set; }
 
        protected int GetReceiveType(string type)
        {
            int t = 1;
            if (type.IsNotNullOrWhiteSpace())
            {
                var p = Params.FirstOrDefault(m => type.Contains(m.ParamText));
                if (!int.TryParse(type, out t))
                {
                    t = 1;
                }
            }
            return t;
        }

        protected string GetBankCode(string currency)
        {
            var model = Banks.FirstOrDefault(m => m.Currency == currency);
            if (model != null)
                return model.BankCode;
            return Banks.FirstOrDefault().BankCode;
        }

        public override IPBankData Parse(System.Data.DataRow row)
        {
            var model = base.Parse(row);

            if (model != null)
            {
                model.BankCode = GetBankCode(model.Currency);
            }
            return model;
        }

        protected override bool Filter(IPBankData model)
        {
            return model.BankPaidIn.HasValue &&model.BankPaidIn!=0 &&model.Payer.IsNotNullOrWhiteSpace();
        }

        protected override string RemoveEmpty(string val)
        {
            return base.RemoveEmpty(val).Replace("\t", "").Replace("\n", "").Trim();
        }
    }
}
